package com.iphaeton.lms.action.organize;

import com.iphaeton.common.excel.IExcelService;
import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.IGroupService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.business.organize.IUserService;
import com.iphaeton.lms.pojo.organize.Group;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.lms.pojo.organize.User;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.dom4j.Document;
import org.dom4j.io.XMLWriter;

public class OrganizeTxAction extends OrganizeBizSupportAction
{
  private static Logger logger;
  private DynaActionForm daf;
  private Pagination pageInfo;
  
  public ActionForward organizeForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    String id = request.getParameter("id");
    try
    {
      org = this.organize.findOrganizeById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("organize", org);
    return mapping.findForward("organize_create");
  }

  public ActionForward addOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    this.daf = ((DynaActionForm)form);
    String name = (String)this.daf.get("name");
    String comment = (String)this.daf.get("comment");
    String parentId = (String)this.daf.get("parentId");
    char type = ((String) (daf.get("type"))).charAt(0);
    Organize org = new Organize();
    Organize parentOrg = null;
    org.setName(name);
    org.setComment(comment);
    org.setType(type);
    try
    {
      parentOrg = this.organize.findOrganizeById(parentId);
      org = this.organize.addOrganize(parentOrg, org);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("name", parentOrg.getName());
    request.setAttribute("organize", org);
    return mapping.findForward("organizeCreateOk");
  }

  public ActionForward selectOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    this.daf = ((DynaActionForm)form);
    int currentPage = 0;
    int pageSize = 5;
    String id = request.getParameter("id");
    String page = (String)this.daf.get("page");
    String row = request.getParameter("row");
    if (row == null) {
      row = "";
    }

    Organize org = null;
    List list = null;
    boolean flag = false;
    try {
      if ((id == null) || ("".equals(id)))
        id = ((UserSession)request.getSession().getAttribute(
          "userSession")).getManageOrgId
          ();
      org = this.organize.findOrganizeById(id);
      flag = this.organize.isRoot(org);
      if ((page == null) || (page.trim().equals("")))
        currentPage = 1;
      else
        currentPage = Integer.parseInt(page);
      this.pageInfo = this.organize.findChildOrganizePagination(id, row, pageSize);
      this.pageInfo.setPage(currentPage);
      this.pageInfo.setRow(row);
      list = this.organize.findPageList(this.pageInfo);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }

    request.getSession().setAttribute("pageInfo", this.pageInfo);
    request.setAttribute("flag", flag);
    request.setAttribute("organize", org);
    request.setAttribute("list", list);
    return mapping.findForward("organizeList");
  }

  public ActionForward editOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("id");
    Organize org = null;
    try {
      org = this.organize.findOrganizeById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("organize", org);
    return mapping.findForward("organizeEdit");
  }

  public ActionForward updateOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    int currentPage = 1;
    int pageSize = 5;
    this.daf = ((DynaActionForm)form);
    String id = (String)this.daf.get("id");
    String name = (String)this.daf.get("name");
    String comment = (String)this.daf.get("comment");

    char type = ((String) (daf.get("type"))).charAt(0);
    Organize org = null;
    List list = null;
    try {
      if ((id != null) && (!("".equals(id))))
        org = this.organize.findOrganizeById(id);
      String oldName = org.getName();
      org.setName(name);
      if (!(oldName.equals(name))) {
        String url = "";
        String oldUrl = org.getUrl();
        if (this.organize.isRoot(org))
          url = name;
        else
          url = org.getParentOrganize().getUrl() + "/" + name;

        this.organize.changeUrl(oldUrl, url);
      }
      org.setComment(comment);

      if (type >= org.getParentOrganize().getType()) {
        List child = this.organize.findChildOrganize(org);
        Iterator it = child.iterator();
        if (it.hasNext()) {
          Organize temp = (Organize)it.next();
          if (type > temp.getType())
            return mapping.findForward("failure");
        }
        org.setType(type);
      } else {
        return mapping.findForward("failure");
      }
      org = this.organize.updateOrganize(org);
      this.pageInfo = this.organize.findChildOrganizePagination(id, "", pageSize);
      this.pageInfo.setPage(currentPage);
      list = this.organize.findPageList(this.pageInfo);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }

    request.getSession().setAttribute("pageInfo", this.pageInfo);
    request.setAttribute("organize", org);
    request.setAttribute("list", list);
    return mapping.findForward("organizeList");
  }

  public ActionForward removeOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    int currentPage = 0;
    int pageSize = 5;
    String id = request.getParameter("id");
    String page = request.getParameter("page");
    String row = request.getParameter("row");
    if (row == null)
      row = "";

    Organize org = null;
    String parentId = null;
    String name = null;
    try {
      org = this.organize.findOrganizeById(id);
      parentId = org.getParentId();
      name = org.getName();
      if ((!(this.organize.isLeaf(org))) || (this.organize.hasUser(org))) {
        return mapping.findForward("error");
      }

      this.organize.remove(id);
      currentPage = 1;
      this.pageInfo = this.organize.findChildOrganizePagination
        (parentId, row, 
        pageSize);
      this.pageInfo.setPage(currentPage);
      this.pageInfo.setRow(row);

      request.getSession().setAttribute("pageInfo", this.pageInfo);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("name", name);
    request.setAttribute("parentId", parentId);
    request.setAttribute("currPage", page);
    return mapping.findForward("organizeRemoveOk");
  }

  public ActionForward getTreeRoot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    String type = request.getParameter("type");
    try
    {
      UserSession userSession = (UserSession)request.getSession().getAttribute
        ("userSession");
      String id = userSession.getManageOrgId();
      org = this.organize.findOrganizeById(id);

      doc = this.organize.getTreeRoot(request.getContextPath(), org, type);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward getTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    String id = request.getParameter("id");
    String type = request.getParameter("type");
    try
    {
      org = this.organize.findOrganizeById(id);
      if ("organize".equals(type)) {
        doc = this.organize.getOrganizeTree(request.getContextPath(), org); }
      else if ("u".equals(type))
      {
        doc = this.organize.getGroupTree(request.getContextPath(), org); }
      else if ("train".equals(type)) {
        doc = this.organize.getTrainTree(org); }
      else if (("user".equals(type)) || ("move".equals(type)) || 
        ("od".equals(type)) || ("op".equals(type))) {
        doc = this.organize.getUserTree(request.getContextPath(), org, type); }
      else if ("course".equals(type))
    	  doc = this.organize.getCourseTree(request.getContextPath(), org);
      
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    label290: response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward getUserSelectTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    String id = request.getParameter("id");
    try {
      Organize org = this.organize.findOrganizeById(id);
      doc = this.organize.getUserSelectOrganizeTree(request.getContextPath(), 
        org);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward getOrganizeSelectTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    String id = request.getParameter("id");
    try {
      Organize org = this.organize.findOrganizeById(id);
      doc = this.organize.getOrganizeSelectOrganizeTree(
        request.getContextPath(), org);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward selectGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    this.daf = ((DynaActionForm)form);
    int currentPage = 0;
    int pageSize = 10;
    String page = (String)this.daf.get("page");
    String id = request.getParameter("id");
    String t = request.getParameter("type");
    char type = t.charAt(0);
    String strMapping = "groupList";
    List list = null;
    Organize org = null;
    String row = request.getParameter("row");
    if (row == null)
      row = "";
    try {
      if ((id == null) || ("".equals(id)))
        id = ((UserSession)request.getSession().getAttribute(
          "userSession")).getManageOrgId
          ();
      org = this.organize.findOrganizeById(id);
      if ((page != null) && (!(page.trim().equals(""))))
        currentPage = Integer.parseInt(page);
      else
        currentPage = 1;

      this.pageInfo = this.organize.findGroupPagination(id, type, row, pageSize);
      this.pageInfo.setPage(currentPage);
      this.pageInfo.setRow(row);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }

    request.getSession().setAttribute("pageInfo", this.pageInfo);

    list = this.organize.findPageList(this.pageInfo);

    if (type == 'u') {
      request.setAttribute("organize", org);
      request.setAttribute("group", list);
    }
    if (type == 'z') {
      request.setAttribute("duty", list);
      strMapping = "dutyList";
    }
    if (type == 'g') {
      request.setAttribute("station", list);
      strMapping = "stationList";
    }
    return mapping.findForward(strMapping);
  }

  public ActionForward removeGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    this.daf = ((DynaActionForm)form);
    int pageSize = 5;
    String page = (String)this.daf.get("page");
    String organize_id = "";
    String[] id = request.getParameter("id").split(";");
    char type = ' ';
    String name = "";
    Group g = null;
    String row = request.getParameter("row");
    if (row == null)
      row = "";
    try {
      for (int i = 0; i < id.length; ++i) {
        g = this.group.findGroupById(id[i]);
        type = g.getType();
        name = g.getName();
        organize_id = g.getOrganizeId();
        this.group.remove(id[i]);
      }
      this.pageInfo = this.organize.findGroupPagination
        (organize_id, type, row, 
        pageSize);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }

    this.pageInfo.setRow(row);

    request.setAttribute("groupName", name);
    request.setAttribute("organizeId", organize_id);
    request.setAttribute("groupType", type);
    request.setAttribute("currPage", page);
    return mapping.findForward("groupRemoveOk");
  }

  public ActionForward changeUserState(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    this.daf = ((DynaActionForm)form);
    String page = (String)this.daf.get("page");
    String id = request.getParameter("id");
    String state = request.getParameter("state");
    String[] uId = request.getParameter("userId").split(";");
    List userList = new LinkedList();
    char userState = ' ';
    if ("a".equals(state))
      userState = 'p';
    else
      userState = 'a';
    try {
      for (int i = 0; i < uId.length; ++i) {
        User u = this.user.findUserById(uId[i]);
        u.setState(userState);
        u = this.user.save(u);
        userList.add(u);
      }
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("state", state);
    request.setAttribute("pageId", page);
    request.setAttribute("user", userList);
    request.setAttribute("id", id);
    return mapping.findForward("stateChangeOk");
  }

  public ActionForward selectUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    this.daf = ((DynaActionForm)form);
    int currentPage = 0;
    int pageSize = 10;
    String page = (String)this.daf.get("page");
    String id = request.getParameter("id");
    String strState = request.getParameter("state");
    String state = "";
    if (strState != null)
      state = strState.trim();

    List userList = null;
    String row = request.getParameter("row");
    if (row == null)
      row = "";
    try {
      if ((id == null) || ("".equals(id)))
        id = ((UserSession)request.getSession().getAttribute(
          "userSession")).getManageOrgId
          ();
      String url = this.organize.findOrganizeById(id).getUrl();
      if ((page == null) || ("".equals(page.trim())))
        currentPage = 1;
      else {
        currentPage = Integer.parseInt(page);
      }

      this.pageInfo = 
        this.organize.findAllUserPagination(url, state, row, pageSize);

      this.pageInfo.setPage(currentPage);
      this.pageInfo.setRow(row);

      userList = this.organize.findPageList(this.pageInfo);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }

    request.getSession().setAttribute("pageInfo", this.pageInfo);
    request.setAttribute("state", state);
    request.setAttribute("id", id);
    request.setAttribute("user", userList);
    return mapping.findForward("user_list");
  }

  public ActionForward removeUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    String page = request.getParameter("page");
    String id = request.getParameter("id");
    String uId = request.getParameter("userId");
    String state = request.getParameter("state");
    User u = null;
    try {
      u = this.user.findUserById(uId);
      if ((uId != null) || (!"".equals(uId))){
    	  this.user.remove(u);
      }
      
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    label111: request.setAttribute("state", state);
    request.setAttribute("type", "remove");
    request.setAttribute("pageId", page);
    request.setAttribute("id", id);
    request.setAttribute("user", u);
    return mapping.findForward("userRemoveOk");
  }

  public ActionForward getUserDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("id");

    Document doc = null;
    try
    {
      doc = this.organize.getUserDocument(this.organize.findOrganizeById(id));
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward getUserSelectTreeRoot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    try {
      UserSession userSession = (UserSession)request.getSession().getAttribute
        ("userSession");
      String id = userSession.getManageOrgId();
      Organize org = this.organize.findOrganizeById(id);
      doc = this.organize.getUserSelectTreeRoot(request.getContextPath(), org);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward getOrganizeSelectTreeRoot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 4702656835960700928L);
    Document doc = null;
    try
    {
      Organize org = this.organize.findRoot();
      doc = this.organize.getOrganizeSelectTreeRoot(request.getContextPath(), 
        org);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    response.setContentType("text/xml; charset=UTF-8");
    ServletOutputStream sos = response.getOutputStream();

    XMLWriter writer = new XMLWriter(sos);
    writer.write(doc);
    writer.flush();
    writer.close();
    return null;
  }

  public ActionForward userMovePrepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    this.daf = ((DynaActionForm)form);

    int currentPage = 0;
    int pageSize = 10;
    String page = (String)this.daf.get("page");
    String type = request.getParameter("type");
    String id = request.getParameter("id");
    String map = "";
    if (type.equals("move"))
      map = "user_move";
    if ((type.equals("duty")) || (type.equals("od")))
      map = "duty_move";
    if ((type.equals("post")) || (type.equals("op")))
      map = "post_move";
    if ((id == null) || ("".equals(id)))
      id = ((UserSession)request.getSession().getAttribute
        ("userSession")).getManageOrgId
        ();
    List userList = null;
    String row = request.getParameter("row");
    if (row == null)
      row = "";
    try {
      if ((page == null) || ("".equals(page.trim())))
        currentPage = 1;
      else
        currentPage = Integer.parseInt(page);

      if ((type.equals("move")) || (type.equals("op")) || (type.equals("od"))) {
        this.pageInfo = this.organize.findUserPagination(id, "", row, pageSize);
        this.pageInfo.setPage(currentPage);
        this.pageInfo.setRow(row);
        userList = this.organize.findPageList(this.pageInfo);
      }
      else{
    	  this.pageInfo = this.group.findUserPagination(id, pageSize);
          this.pageInfo.setPage(currentPage);
          this.pageInfo.setRow(row);
          userList = this.group.findUser(((UserSession)
            request.getSession().getAttribute("userSession")).getManageOrgId
            (), id);
      }
     
    }
    catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }

    label396: request.getSession().setAttribute("pageInfo", this.pageInfo);
    request.setAttribute("type", type);
    request.setAttribute("id", id);
    request.setAttribute("user", userList);
    return mapping.findForward(map);
  }

  public ActionForward userMove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    String id = request.getParameter("id");
    String[] user_id = id.split(";");

    List list = new LinkedList();
    try {
      for (int i = 0; i < user_id.length; ++i) {
        User u = this.user.findUserById(user_id[i]);
        list.add(u);
      }
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("list", list);
    request.setAttribute("user", id);
    return mapping.findForward("user_move_to");
  }

  public ActionForward groupMove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    List list;
    String id = request.getParameter("id");
    String[] user_id = id.split(";");
    String type = request.getParameter("type");
    String map = "";
    if (type.equals("z"))
      map = "duty_move_to";
    if (type.equals("g"))
      map = "post_move_to";
    User u = null;

    List userList = new LinkedList();
    Group g = new Group();
    try {
      for (int i = 0; i < user_id.length; ++i) {
        u = this.user.findUserById(user_id[i]);
        userList.add(u);
      }
      
      list = this.group.getGroupList(type, "");
      List temp = this.user.findGroup(u, type);
      if (temp != null && temp.size()> 0){
    	  g = (Group)this.user.findGroup(u, type).get(0);
      }
      
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    label226: request.setAttribute("id", id);
    request.setAttribute("list", list);
    request.setAttribute("user", userList);
    request.setAttribute("group", g);
    return mapping.findForward(map);
  }

  public ActionForward userMoveOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    String id = request.getParameter("id");
    String user_id = request.getParameter("user_id");
    String[] u_id = user_id.split(";");
    List list = new LinkedList();
    try {
      for (int i = 0; i < u_id.length; ++i)
      {
        this.organize.moveOrganize(u_id[i], id);
        User u = this.user.findUserById(u_id[i]);

        list.add(u);
      }
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("list", list);
    return mapping.findForward("user_move_ok");
  }

  public ActionForward userMoveGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    String id = request.getParameter("id");
    String[] user_id = request.getParameter("user_id").split(";");
    User u = null;
    Group g = null;
    List list = new LinkedList();
    try {
      for (int i = 0; i < user_id.length; ++i) {
        u = this.user.findUserById(user_id[i]);
        g = this.group.findGroupById(id);
        List temp = user.findGroup(u, (new StringBuffer(String.valueOf(g.getType()))).toString());
        
        if ((temp != null) && (temp.size() > 0))
          this.group.removeUser((Group)temp.get(0), user_id[i]);
        g = this.group.addUser(g, u);
        list.add(u);
      }
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("list", list);
    request.setAttribute("group", g);
    return mapping.findForward("user_group_move_ok");
  }

  public ActionForward exportTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list;
    String name = request.getRealPath("/") + "template/organize.xls";
    try
    {
      UserSession userSession = (UserSession)request.getSession().getAttribute
        ("userSession");
      String manageId = userSession.getManageOrgId();
      list = this.organize.findAllChildOrganize(
        this.organize.findOrganizeById(manageId));
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("list", list);
    request.setAttribute("name", name);
    return mapping.findForward("organize_template");
  }

  public ActionForward importOrganizeInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    this.daf = ((DynaActionForm)form);
    String dataFile = (String)this.daf.get("name");

    List list = null;
    try {
      list = this.excel.readExcel(dataFile);

      for (int i = 0; i < list.size(); ++i) {
        Organize org = new Organize();
        List temp = (List)list.get(i);
        org.setParentId(
          this.organize.findOrganizeByUrl(temp.get(0).toString().trim()).getId());
        org.setName(temp.get(1).toString().trim());
        org.setComment((temp.get(2) != null) ? 
          temp.get(2).toString().trim() : null);
        if (temp.get(3).toString().trim().equals("公司"))
          org.setType('c');
        else if (temp.get(3).toString().trim().equals("部门"))
          org.setType('d');
        org.setUrl(temp.get(0).toString().trim() + "/" + 
          temp.get(1).toString().trim());
        org = this.organize.createOrganize(org);
        logger.info("导入组织机构: " + org.getName() + " 成功!");
      }
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    return mapping.findForward("import_complete");
  }

  public ActionForward organizeMovePrepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    String id = request.getParameter("id");
    try
    {
      org = this.organize.findOrganizeById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("organize", org);
    return mapping.findForward("organize_move_to");
  }

  public ActionForward organizeMove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    Organize parentOrg;
    String organize_id = request.getParameter("organize_id");
    String organizeId = request.getParameter("id");

    String po = "";
    try {
      org = this.organize.findOrganizeById(organize_id);
      parentOrg = this.organize.findOrganizeById(organizeId);
      po = org.getParentOrganize().getUrl();
      if ((parentOrg.getType() == 'd') && (org.getType() == 'c'))
        throw new BusinessException("不能调整公司类型组织机构到部门类型的组织机构下，请检查后重新操作!");

      if (this.organize.isChild(parentOrg, org))
        throw new BusinessException("不能调整组织机构到自己的子机构，请检查后重新操作!");

      this.organize.changeUrl(org.getUrl(), parentOrg.getUrl() + "/" + 
        org.getName());
      org.setParentId(organizeId);
      this.organize.updateOrganize(org);
    }
    catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("organize", org);
    request.setAttribute("po", po);
    request.setAttribute("newPo", parentOrg.getUrl());
    return mapping.findForward("organize_move_ok");
  }

  public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String name = request.getParameter("name");
    String url = request.getParameter("url");
    url = request.getRealPath("/") + url;
    request.setAttribute("url", url);
    request.setAttribute("name", name);
    return mapping.findForward("download");
  }

  public ActionForward userCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Organize org;
    int active_user = 0;
    int pause_user = 0;
    int new_user = 0;
    try {
      String organzie_id = ((UserSession)request.getSession().getAttribute
        ("userSession")).getManageOrgId
        ();
      org = this.organize.findOrganizeById(organzie_id);
      active_user = this.organize.findAllUser(org, 'a').size();
      pause_user = this.organize.findAllUser(org, 'p').size();
      new_user = this.organize.findAllUser(org, 'g').size();
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("total_user", 
      new Integer(active_user + pause_user + 
      new_user));
    request.setAttribute("active_user", new Integer(active_user));
    request.setAttribute("pause_user", new Integer(pause_user));
    request.setAttribute("new_user", new Integer(new_user));
    return mapping.findForward("user_count");
  }
}